#include <QFile>
#include <QDebug>
#include "soundmem.h"

SoundMem::SoundMem()
{

}

SoundMem::~SoundMem()
{

}


void SoundMem::readSoundmemXml(QString filename)
{
    QFile file(filename);
    // default initial prog memory
    if(!file.exists()) {
        filename=":/conf/sound.xml";
        file.setFileName(filename);
    }
    //qDebug() << "reading progmem " << filename;
    if (!file.open(QFile::ReadOnly | QFile::Text)) {
        //qDebug() << "cannot read file " << filename;
        return;
    }
    xmlr.setDevice(&file);
    if (xmlr.readNextStartElement()) {
        if (xmlr.name() == "misucosoundmem" && xmlr.attributes().value("version") == "1.0") {
            int row=0;
            while (xmlr.readNextStartElement() && row<soundmem_max) {
                //qDebug() << "row " << row;
                if (xmlr.name() == "sound") {
                    for(int j=0;j<soundparam_max;j++) {
                        QString attname;
                        attname.sprintf("p%d",j);
                        if(j==0) {
                            soundParam[row][j]=xmlr.attributes().value(attname).toString().toInt()/4.0f;
                        } else {
                            soundParam[row][j]=xmlr.attributes().value(attname).toString().toInt()/127.0f;
                        }
                        //qDebug() << "1.0 sound param j " << j << " " << soundParam[row][j];
                    }
                    xmlr.skipCurrentElement();
                    row++;
                } else {
                    xmlr.skipCurrentElement();
                }
            }
        } else if (xmlr.name() == "misucosoundmem" && xmlr.attributes().value("version") == "1.1") {
            int row=0;
            while (xmlr.readNextStartElement() && row<soundmem_max) {
                //qDebug() << "row " << row;
                if (xmlr.name() == "sound") {
                    for(int j=0;j<soundparam_max;j++) {
                        QString attname;
                        attname.sprintf("p%d",j);
                        soundParam[row][j]=xmlr.attributes().value(attname).toFloat();
                        //qDebug() << "1.1 sound param j " << j << " " << soundParam[row][j];
                    }
                    xmlr.skipCurrentElement();
                    row++;
                } else {
                    xmlr.skipCurrentElement();
                }
            }
        } else {
            xmlr.raiseError(QObject::tr("The file is not a MISUCO version 1.0/1.1 file."));
        }
    }
    file.close();
}

void SoundMem::writeSoundmemXml(QString filename)
{
    QFile file(filename);
    if (!file.open(QFile::WriteOnly | QFile::Text)) {
        qDebug() << "cannot write file " << filename;
        return;
    }
    //qDebug() << "writing soundmem file " << filename;

    xml.setDevice(&file);
    QString att;
    QString attname;

    xml.writeStartDocument();
    xml.writeDTD("<!DOCTYPE misuco>");
    xml.writeStartElement("misucosoundmem");
    xml.writeAttribute("version", "1.1");

    for (int row = 0; row < soundmem_max; row++) {
        xml.writeStartElement("sound");

        for(int j=0;j<soundparam_max;j++) {
            att.sprintf("%f",soundParam[row][j]);
            attname.sprintf("p%d",j);
            xml.writeAttribute(attname,att);
        }

        xml.writeEndElement();
    }
    xml.writeEndDocument();
    file.close();
}
